import java.util.Arrays;

// 循环队列类
class CircularQueue {
    private int[] arr;
    private int front;
    private int rear;
    private int size;

    // 构造函数，初始化循环队列
    public CircularQueue(int capacity) {
        arr = new int[capacity];
        front = 0;
        rear = 0;
        size = 0;
    }

    // 判断队列是否已满
    public boolean isFull() {
        return (rear + 1) % arr.length == front;
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    // 入队操作
    public void enqueue(int value) {
        if (isFull()) {
            throw new IllegalStateException("Queue is full");
        }
        arr[rear] = value;
        rear = (rear + 1) % arr.length;
        size++;
    }

    // 出队操作，返回出队的元素
    public int dequeue() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        int value = arr[front];
        front = (front + 1) % arr.length;
        size--;
        return value;
    }

    // 获取队列大小
    public int getSize() {
        return size;
    }

    // 正确的peek方法实现
    public int peek() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        return arr[front];
    }
}

// 主类，用于打印杨辉三角
public class YangHuiTriangle {
    // 打印杨辉三角的方法
    public static void printYangHuiTriangle(int n) {
        for (int i = 0; i < n; i++) {
            CircularQueue queue = new CircularQueue(i + 3);
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    queue.enqueue(1);
                } else {
                    int num1 = queue.dequeue();
                    int num2 = queue.getSize() > 0? queue.peek() : 1;
                    queue.enqueue(num1 + num2);
                    // 在计算完中间元素并重新入队后再出队
                    queue.dequeue();
                }
            }
            int[] row = new int[i + 1];
            for (int k = 0; k <= i; k++) {
                row[k] = queue.dequeue();
            }
            System.out.println(Arrays.toString(row));
        }
    }

    public static void main(String[] args) {
        printYangHuiTriangle(5);
    }
}